package com.example.administrator.szb.view;

import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.example.administrator.szb.R;
import com.example.administrator.szb.util.Contants;
import com.example.administrator.szb.util.Toasts;

/**
 * Created by stone on 2018/1/23.
 */

public class LuYanBaoMingPop {
    private static PopupWindow popupWindow;
    private static PopupWindow popupWindow2;
    private static Activity context;

    public interface OnEditResultListener{
        void onResult(String uname, String phone);
    }

    public static void showInfo(Activity ctx, View view, final String names, String tels, final OnEditResultListener l){
        context  = ctx;
        final View popView = LayoutInflater.from(context).inflate(
                R.layout.baoming_confirm, null);
        TextView rightBtn = popView.findViewById(R.id.rightBtn);
        TextView leftBtn = popView.findViewById(R.id.leftBtn);
        final EditText name = popView.findViewById(R.id.name);
        final EditText tel = popView.findViewById(R.id.tel);
        name.setText(names);
        tel.setText(tels);
        rightBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(name.getText().toString().trim())){
                    Toasts.show(context,name.getHint().toString());
                    return;
                }
                if (!Contants.isMobileNO(tel.getText().toString().trim())){
                    Toasts.show(context,"手机号格式不正确");
                    return;
                }
                if(l!=null){
                    l.onResult(name.getText().toString().trim(),tel.getText().toString().trim());
                }
            }
        });
        leftBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });
        popupWindow = new PopupWindow(popView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        setBackgroundAlpha(0.5f);
        ColorDrawable dw = new ColorDrawable(context.getResources().getColor(R.color.white));
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setFocusable(true);// 点击空白处时，隐藏掉pop窗口
        popupWindow.setClippingEnabled(false);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                setBackgroundAlpha(1.0f);
            }
        });
        popupWindow.showAtLocation(view, Gravity.CENTER,0,0);
    }
    public static void showSuccess(Activity ctx,View view){
        context  = ctx;
        final View popView = LayoutInflater.from(context).inflate(
                R.layout.baoming_success, null);
        Button btn_close = popView.findViewById(R.id.btn_close);
        ImageView iv_close = popView.findViewById(R.id.iv_close);
        btn_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow2.dismiss();
            }
        });
        iv_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow2.dismiss();
            }
        });
        popupWindow2 = new PopupWindow(popView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        setBackgroundAlpha(0.5f);
        ColorDrawable dw = new ColorDrawable(context.getResources().getColor(R.color.white));
        popupWindow2.setBackgroundDrawable(new BitmapDrawable());
        popupWindow2.setFocusable(true);// 点击空白处时，隐藏掉pop窗口
        popupWindow2.setClippingEnabled(false);
        popupWindow2.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                setBackgroundAlpha(1.0f);
            }
        });
        popupWindow2.showAtLocation(view, Gravity.CENTER,0,0);
    }

    public static void hide(int n){
        if (n==0){
            if (popupWindow!=null){
                popupWindow.dismiss();
            }
        }else {
            if (popupWindow2!=null){
                popupWindow2.dismiss();
            }
        }
    }


    public static void setBackgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = (context).getWindow()
                .getAttributes();
        lp.alpha = bgAlpha;
        ((Activity) context).getWindow().setAttributes(lp);
    }
}
